package com.fary.context.weaving;


import com.fary.beans.factory.BeanFactory;
import com.fary.beans.factory.BeanFactoryAware;
import com.fary.beans.factory.config.BeanPostProcessor;
import com.fary.context.ConfigurableApplicationContext;
import com.fary.core.SpringException;
import com.fary.instrument.classloading.LoadTimeWeaver;
import com.fary.util.Assert;

public class LoadTimeWeaverAwareProcessor implements BeanPostProcessor, BeanFactoryAware {

	private LoadTimeWeaver loadTimeWeaver;

	private BeanFactory beanFactory;

	public LoadTimeWeaverAwareProcessor() {
	}

	public LoadTimeWeaverAwareProcessor(LoadTimeWeaver loadTimeWeaver) {
		this.loadTimeWeaver = loadTimeWeaver;
	}

	public LoadTimeWeaverAwareProcessor(BeanFactory beanFactory) {
		this.beanFactory = beanFactory;
	}


	@Override
	public void setBeanFactory(BeanFactory beanFactory) {
		this.beanFactory = beanFactory;
	}


	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws SpringException {
		if (bean instanceof LoadTimeWeaverAware) {
			LoadTimeWeaver ltw = this.loadTimeWeaver;
			if (ltw == null) {
				Assert.state(this.beanFactory != null, "BeanFactory required if no LoadTimeWeaver explicitly specified");
				ltw = this.beanFactory.getBean(ConfigurableApplicationContext.LOAD_TIME_WEAVER_BEAN_NAME, LoadTimeWeaver.class);
			}
			((LoadTimeWeaverAware) bean).setLoadTimeWeaver(ltw);
		}
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String name) {
		return bean;
	}

}